package bar.dark.lee.alarm.effect;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.SuperActivity;
import bar.dark.lee.alarm.effect.buttonrush.EffectButtonRushActivity;
import bar.dark.lee.alarm.effect.question.EffectQuestionActivity;
import bar.dark.lee.alarm.effect.shake.EffectShakeActivity;
import bar.dark.lee.alarm.effect.typing.EffectTypingActivity;

/**
 * 효과 리스트 화면
 * @author HyoJun Lee
 *
 */
public class EffectListActivity extends SuperActivity
{
	public static final String EFFECT_SELECT = "effect_select";
	public static final String EFFECT_PLAY = "effect_play";
	
	public static final int EFFECT0 = 0;
	public static final int EFFECT1 = 1;
	public static final int EFFECT2 = 2;
	public static final int EFFECT3 = 3;
	public static final int EFFECT4 = 4;
	public static final int EFFECT5 = 5;
	
	private ListView listViewEffect;
	private Button btnOk;
	
	private ArrayList<EffectData> listEffect;
	private ArrayList<Integer> listEffectDefaultId;
	private ArrayList<Integer> listEffectSelectId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.effectlist_activity);
		
		setTitle(getString(R.string.effectlist_title));
		
		setFindView();
		setDataSetting();
		setClickListener();
	}

	@Override
	public void setFindView()
	{
		listViewEffect = (ListView) findViewById(R.id.effectlist_list);
		btnOk = (Button) findViewById(R.id.effectlist_btnOk);
	}

	@Override
	public void setDataSetting()
	{
		listEffect = new ArrayList<EffectData>();
		
		listEffectDefaultId = new ArrayList<Integer>();
		listEffectDefaultId.add(EFFECT0);
		listEffectDefaultId.add(EFFECT1);
		listEffectDefaultId.add(EFFECT2);
		listEffectDefaultId.add(EFFECT3);
		listEffectDefaultId.add(EFFECT4);
		listEffectDefaultId.add(EFFECT5);
		
		Intent intent = getIntent();
		listEffectSelectId = intent.getIntegerArrayListExtra(EFFECT_SELECT);
		
		if (listEffectSelectId == null)
		{
			listEffectSelectId = new ArrayList<Integer>();
		}
		
		setList(listEffectSelectId);
		
		EffectListAdapter adapter = new EffectListAdapter(this, R.layout.list_item_effect, listEffect);
		listViewEffect.setAdapter(adapter);
		listViewEffect.setOnItemClickListener(adapter);
		listViewEffect.setDividerHeight(1);
		adapter.notifyDataSetChanged();
	}

	private void setList(ArrayList<Integer> list)
	{
		boolean flagSelect = false;
		
		for (int i=0; i<listEffectDefaultId.size(); i++)
		{
			EffectData data = new EffectData();
			
			for (int j=0; j<list.size(); j++)
			{
				if (i == list.get(j))
				{
					flagSelect = true;
				}
			}
			
			if (flagSelect)
			{
				data.setCheck(true);
				flagSelect = false;
			}
			else
			{
				data.setCheck(false);
			}
			
			switch (listEffectDefaultId.get(i)) 
			{
				case EFFECT0:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_0_title));
					data.setContent(gets(R.string.effectlist_0_content));
					
					listEffect.add(data);
					
					break;
				case EFFECT1:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_1_title));
					data.setContent(gets(R.string.effectlist_1_content));
					
					listEffect.add(data);
					
					break;
				case EFFECT2:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_2_title));
					data.setContent(gets(R.string.effectlist_2_content));
					
					listEffect.add(data);
					
					break;
				case EFFECT3:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_3_title));
					data.setContent(gets(R.string.effectlist_3_content));
					
					listEffect.add(data);
					
					break;
				case EFFECT4:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_4_title));
					data.setContent(gets(R.string.effectlist_4_content));
					
					listEffect.add(data);
					
					break;
				case EFFECT5:
					
					data.setImgUri("");
					data.setTitle(gets(R.string.effectlist_5_title));
					data.setContent(gets(R.string.effectlist_5_content));
					
					listEffect.add(data);
					
					break;
				default:
					break;
			}
		}
	}
	
	@Override
	public void setClickListener()
	{
		btnOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			
				listEffectSelectId.clear();
				
				for (int i=0; i<listEffect.size(); i++)
				{
					if (listEffect.get(i).isCheck())
					{
						listEffectSelectId.add(i);
					}
				}
				
				Intent intent = new Intent();
				intent.putIntegerArrayListExtra(EFFECT_SELECT, listEffectSelectId);
				setResult(RESULT_OK, intent);
				
				finish();
			}
		});
	}

	@Override
	protected void tipButton()
	{
	}

	private class EffectListAdapter extends ArrayAdapter<EffectData> implements OnItemClickListener
	{
		private Context context;
		private int viewId;
		private ArrayList<EffectData> list;
		
		public EffectListAdapter(Context context, int textViewResourceId, ArrayList<EffectData> list) 
		{
			super(context, textViewResourceId, list);
			this.context = context;
			this.viewId = textViewResourceId;
			this.list = list;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v  = li.inflate(viewId, null);
			}
			
			EffectData data = list.get(position);
			
			View imgFront = v.findViewById(R.id.effectlist_item_frontimg);
			View imgBack = v.findViewById(R.id.effectlist_item_backimg);
			TextView textTitle = (TextView) v.findViewById(R.id.effectlist_item_textTitle);
			TextView textContent = (TextView) v.findViewById(R.id.effectlist_item_textContent);
			View imgPlay = v.findViewById(R.id.effectlist_item_playimg);
			
			if (data.isCheck())
			{
				imgBack.setBackgroundColor(Color.RED);
			}
			else
			{
				imgBack.setBackgroundColor(Color.BLUE);
			}
			textTitle.setText(data.getTitle());
			textContent.setText(data.getContent());
			
			imgPlay.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					Intent intent = new Intent();

					switch (position) 
					{
						case 0:
							intent.setClass(superContext, EffectQuestionActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT0);
							break;
						case 1:
							intent.setClass(superContext, EffectTypingActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT1);
							break;
						case 2:
							intent.setClass(superContext, EffectButtonRushActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT2);
							break;
						case 3:
							intent.setClass(superContext, EffectShakeActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT3);
							break;
						case 4:
							intent.setClass(superContext, EffectQuestionActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT0);
							break;
						case 5:
							intent.setClass(superContext, EffectQuestionActivity.class);
							intent.putExtra(EFFECT_PLAY, EFFECT0);
							break;
						default:
							break;
					}
					
					startActivity(intent);
				}
			});
			
			return v;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
			
			View imgBack = v.findViewById(R.id.effectlist_item_backimg);
			
			if (listEffect.get(position).isCheck())
			{
				imgBack.setBackgroundColor(Color.BLUE);
				listEffect.get(position).setCheck(false);
			}
			else
			{
				imgBack.setBackgroundColor(Color.RED);
				listEffect.get(position).setCheck(true);
			}
		}
	}
}
